// pages/my/my.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    login:1,
    captcha:"获取验证码",
    phone:"",
    broker:[],
    menu:[
      {"image":'http://hhh.guobaochunlove.xyz/feiji.png',"text":'消息','skip':'message'},
      {"image":'http://hhh.guobaochunlove.xyz/dianpu.png',"text":'求租','skip':'wanted'},
      {"image":'http://hhh.guobaochunlove.xyz/fangzi.png',"text":'求购','skip':'buy'},
      {"image":'http://hhh.guobaochunlove.xyz/dengta.png',"text":'楼市圈','skip':'brokercircle'},
      {"image":'http://hhh.guobaochunlove.xyz/caifang.png',"text":'直播','skip':'live'},
      {"image":'http://hhh.guobaochunlove.xyz/dianpupinggu.png',"text":'团购','skip':'grouppurchase'},
      {"image":'http://hhh.guobaochunlove.xyz/jingbei.png',"text":'排行榜','skip':'rankinglist'},
      {"image":'http://hhh.guobaochunlove.xyz/haitun.png',"text":'战绩','skip':'record'},
      {"image":'http://hhh.guobaochunlove.xyz/haitun.png',"text":'人脸识别','skip':'face'}
    ],
    loginclass:0,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    src: "", //图片的链接
    baidutoken: "",
    base64: "",
    msg: ""
  },
  getphone(e){
    this.setData({
      phone:e.detail.value
    })
},
getVerificationCode(e){
  this.setData({
    verification:e.detail.value
  })
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
 let islogin = wx.getStorageSync('islogin')
      let that = this
      if (islogin) {
        let token = wx.getStorageSync('token')
        that.setData({
          login:0,
        })
        wx.request({
          url: 'http://www.house.com/index.php/api/brokermessage',
          header:{
              token:token
          },
          success:res=>{
            this.setData({
              login:0,
              broker:res.data.data
            })
          }
      })
          }else{
        that.setData({
          login:1
        })
      }
  },
  getCaptcha(){
    let phone = this.data.phone
    let that = this
    if (!phone) {
      wx.showToast({
        title: '请输入手机号',
        icon:"error"
      })
      return false
    }
    let time=60
    let countdown = setInterval(function(){
        time--
        
        if (time>0) {
          that.setData({
            captcha:time+"秒后获取",
            disabled:1
          })
        }else{
          that.setData({
            captcha:"获取验证码",
            disabled:0
          })
          clearInterval(countdown)
        }
       
    },1000) 
   
    wx.request({
      url:"http://www.house.com/index.php/api/getcaptcha",
      data:{
        phone:phone
      },
      method:"POST",
      success:res=>{
        console.log(res)
      }
    })
  },
  userLogin(){
    let phone = this.data.phone
    let verification = this.data.verification
    if (!phone) {
      wx.showToast({
        title: '请输入手机号',
        icon:"error"
      })
      return false
    }
    if (!verification) {
      wx.showToast({
        title: '请输入验证码',
        icon:"error"
      })
      return false
    }
    wx.login({
      success:res=>{
        wx.request({
          url:"http://www.house.com/index.php/api/phonelogin",
          data:{
            phone:phone,
            verification:verification,
            code:res.code
          },
          method:"POST",
          success:res=>{
            console.log(res)
            if (res.data.code==200) {
              wx.setStorageSync('islogin', true,86400)
              wx.setStorageSync('token', res.data.data.token,86400)
              wx.showToast({
                title: '登录成功',
                icon:"success"
              })
              this.setData({
                login:0,
                broker:res.data.data,
            
              })
            }else{
              wx.showToast({
                title: '登录失败',
                icon:"error"
              })
            }
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  brokerSkip(e){
      console.log(e)
      let skip = e.currentTarget.dataset.skip
      wx.navigateTo({
        url: '/pages/'+skip+'/'+skip,
      })
  },
  takePhoto() {
    var that = this;
    //拍照
    const ctx = wx.createCameraContext()
    wx.chooseMedia({
      count: 9,
      mediaType: ['image','video'],
      sourceType: ['album', 'camera'],
      maxDuration: 30,
      camera: 'back',
      success(res) {
       
        let tempfilepath = res.tempFiles[0].tempFilePath
        that.setData({
          src: tempfilepath //获取图片
         })
        wx.getFileSystemManager().readFile({
          filePath: tempfilepath, //选择图片返回的相对路径
          encoding: 'base64', //编码格式
          success: res => { //成功的回调
            console.log(res)
            that.uploadPhoto(res.data)
            that.setData({
              base64: res.data
            })
          }
        })
        
      }
    })
    // ctx.takePhoto({
    //   quality: 'high',
    //   success: (res) => {
    //     this.setData({
    //       src: res.tempImagePath //获取图片
    //     })
        //图片base64编码
        // wx.getFileSystemManager().readFile({
        //   filePath: this.data.src, //选择图片返回的相对路径
        //   encoding: 'base64', //编码格式
        //   success: res => { //成功的回调
        //     console.log(res)
        //     this.setData({
        //       base64: res.data
        //     })
        //   }
        // })
    //     // this.getBaiduToken();
    //   } //拍照成功结束
    // }) //调用相机结束

    //失败尝试
    // wx.showToast({
    //   title: '请重试',
    //   icon: 'loading',
    //   duration: 500
    // })
  },
  faceLogin(){
    let that = this
    that.setData({
      loginclass:1
    })
  },
  error(e) {
    console.log(e.detail)
  },
  uploadPhoto(data) {
    var that = this;
    //上传人脸进行识别-----test
    let base = that.data.base64
    wx.request({
      url: 'http://www.house.com/index.php/api/recognition',
      method: 'POST',
      data: {
        image: data,
        type: 'base64',
      },
      success(res) {
        console.log(res)
        that.setData({
          msg: res.data.error_msg
        })
        //做成功判断
        if (that.data.msg == "match user is not found") {
          that.setData({
            loginclass:0
          })
          wx.showToast({
            title: '未人脸识别',
            icon: 'error',
          })
        }
        if (that.data.msg == 'SUCCESS') {
          wx.setStorageSync('islogin', true,86400)
          wx.setStorageSync('token', res.data.data.token,86400)
          that.setData({
            login:0,
            broker:res.data.data,
        
          })
          wx.showToast({
            title: '人脸识别成功',
            icon: 'success',
          })
        }
      }
    })
  }
})